<%@ page import="org.jsoup.Jsoup" %>
<%@ page import="org.jsoup.select.Elements" %>
<%@ page import="org.jsoup.nodes.Element" %>
<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
    // 抓取远程图片到本地服务器，并返回本地服务器地址集合
    public static String imgs(Element content, String domian) {
        Elements imgs = content.select("img");
        List<HashMap<String, String>> imgsJson = new ArrayList<HashMap<String, String>>();
        int size = imgs.size();
        for(int i = 0; i < size; i++) {
            String src = imgs.get(i).attr("src").toString();
            String domianIgnore = domian.replace("www.","");
            if((!src.startsWith(domian)) && (!src.startsWith(domianIgnore))) {
                src = domian + src;
            }

            String big = ImageUtil.big(src);
            String sizeJson = ImageUtil.size(big);
            String pixel  = "550 * 366";
            List<HashMap> sizeList = JSON.parseArray(sizeJson,HashMap.class);
            if(sizeList.size() > 0) {
                HashMap<String, Object> sizeMap = sizeList.get(0);

                pixel = sizeMap.get("width").toString() + " * " + sizeMap.get("height").toString();
            }

            // 存入HashMap中
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ref","!--IMG#" + i + "--");
            map.put("pixel", pixel);
            map.put("src", big);
            // 循环添加某篇新闻
            imgsJson.add(map);
        }
        return JSON.toJSONString(imgsJson);
    }

    // 替换新闻内容中的html标签
    public  String replaceContent(String html) {
        html = Tools.getDesc(html, "");
        html = html.toLowerCase();
        html = html.replaceAll("<p[^>]*?>", "<p>");
        html = html.replaceAll("<div[^>]*?>", "<p>");
        html = html.replaceAll("</div>", "</p>");
        html = html.replaceAll("<font[^>]*?>", "");
        html = html.replaceAll("<b(?!r)[^>]*?>", "");
        html = html.replaceAll("<span[^>]*?>","");
        html = html.replaceAll("</span>","");
        html = html.replaceAll("</font>","");
        html = html.replaceAll("<br[^>]*?>","<br/>");
        html = html.replaceAll("<style[^>]*?>[\\s\\S]*?<\\/style>","");
        html = html.replaceAll("<a[^>]*?>","");
        html = html.replaceAll("</a>", "");
        html = replaceTitle(html);
        int i = 0;
        while(html.indexOf("<img") > -1) {
            html = html.replaceFirst("<img[^>]*?>", "!--IMG#" + (i++)  + "--");
        }
        return html;
    }
    // 替换抓取新闻标题中可能出现的特殊字符
    public  String replaceTitle(String title) {
        title = Tools.getDesc(title, "");
        title = title.replaceAll("\"", "&quot;").replaceAll(":", "&#58;");
        return title;
    }

    // 替换抓取新闻标题中可能出现的特殊字符2
    public  String replaceTitle2(String title) {
        title = Tools.getDesc(title, "");
        title = title.replaceAll("&nbsp;", "    ").replaceAll("&#58;", ":");
        return title;
    }
%>
<%
    hospital = 1113;
    Integer category = input.getInt("category",1);
    //获取新闻列表
    List<Map> list = QBuilder.db("basedb").queryHql("select new map(url as url , time as time) from News where hospital="+hospital +" and category="+category + " order by time desc");
    Map urlMap = new HashMap();
    for(Map map : list){
        urlMap.put(map.get("url"),map.get("url"));
    }
    Map maxMap = list.get(0);
    String maxDate = maxMap.get("time") == null ? "2017-01-01" : maxMap.get("time").toString();
    int i = 0;
    String url = "http://www.wxxsh.net/new/YuanNaDongTai/Index.html";//首页
   if(category == 53){
        url = "http://www.wxxsh.net/new/YuanNaGongGao/Index.html";//通知公告
    }else  if(category == 54){
        url = "http://www.wxxsh.net/new/HuaShuJiaoLiu/Index.html";//学术交流
    }else  if(category == 55){
        url = "http://www.wxxsh.net/new/JiShuJinZhan/Index.html";//技术进展
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try{
        List<News> newsList = new ArrayList<News>();
        while(true){

            String html = HttpUtil.get(url, "utf-8");
            org.jsoup.nodes.Document doc = Jsoup.parse(html);
            Elements tdElements = doc.select("#pagenav");


            int pageNum = doc.select(".listborder").select("tr").size();
            if(pageNum == 16){//当前页已满

            }


            //遍历每条新闻
            for(int j = 0 ; j < pageNum ; j++){
                News n = new News();
                Elements elements = doc.select(".listborder").select("tr");//找到所有的list集合
                String childUrl = "http://www.wxxsh.net"+elements.get(j).select(".list1").select("a").attr("href");
                String date = elements.get(j).select(".list1").select("font").html() ;
                if(date == null || "".equals(date)){
                    continue;
                }
                if(sdf.parse(maxDate).getTime() > sdf.parse(date).getTime()){
                    break;
                }

                //重复过滤
                if(list != null && list.size() > 0 &&(urlMap.get(childUrl) != null &&  !"".equals(urlMap.get(childUrl))) ){
                    continue;
                }
                n.setUrl(childUrl);


                n.setTime(date);

                String title = replaceTitle2(elements.get(j).select(".list1").select("a").html());
                n.setSummary(title);
                n.setTitle(title);

                //处理进入url获取表

                String childHtml = HttpUtil.get(childUrl, "utf-8");

                org.jsoup.nodes.Document childDoc = Jsoup.parse(childHtml);

                String content = childDoc.select("table").select(".contentborder").select(".nr_1").html();//获取内容
                content = replaceContent(content);
                n.setContent(content);

                String imgs = imgs( childDoc.select("table").select(".contentborder").select(".nr_1").first() , "http://www.wxxsh.net/");		// 把新闻里面的图片取出来

                List<HashMap> imgList = JSON.parseArray(imgs , HashMap.class);
                n.setImg(imgs);
                n.setAuthor("无锡市锡山人民医院");
                n.setcTime(System.currentTimeMillis());
                n.setStatus(1);
                n.setHospital(1113);
                n.setCategory(category);
                n.setJsonString("{}");
                n.setHtml(childDoc.select("table").select(".contentborder").select(".nr_1").html());
                newsList.add(n);

            }
            i++;
            //超过100页或者 下一页的属性为空
            if(i>100 || doc.select("#pagenav").select("a").size() == 0 || !"<font face=\"webdings\">4</font>".equals(doc.select("#pagenav").select("a").last().previousElementSibling().html()) ){
                break;
            }
            //更新url地址
            url = "http://www.wxxsh.net" + doc.select("#pagenav").select("a").last().previousElementSibling().attr("href");
         }
         if(newsList == null || newsList.size() == 0){

         }else{
             Collections.reverse(newsList);
             DBTools.db("basedb").addItem(newsList);
             QBuilder.db("basedb").execHQL("update News set newsId  = id where hospital = 1113 and category ="+category);
         }


        out.println("执行完毕");
    }catch (Exception e){
        e.printStackTrace();
    }
%>
